package cn.stu.framework.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stu.common.utils.ApiData;
import cn.stu.framework.dto.LogDto;
import cn.stu.framework.entity.Log;
import cn.stu.framework.mapper.LogMapper;
import cn.stu.framework.service.LogService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 日志服务实现类
 *
 * @author 郑志豪
 * @date 2023/09/14
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public void insert(Log log) {
        logMapper.insert(log);
    }

    @Override
    public ApiData logList(LogDto dto) {
        if (dto.getLimit() == null || dto.getPage() == null) {
            return ApiData.getFalse("limit或page为空");
        }
        Page<Log> page = new Page<>(dto.getPage(), dto.getLimit());
        lambdaQuery().like(StrUtil.isNotBlank(dto.getRequestType()), Log::getRequestType, dto.getRequestType())
                .like(StrUtil.isNotBlank(dto.getAction()), Log::getAction, dto.getAction())
                .like(StrUtil.isNotBlank(dto.getRequestIp()), Log::getRequestIp, dto.getRequestIp())
                .orderByDesc(Log::getCreateTime).page(page);
        return ApiData.getPageTrue("获取成功", page.getTotal(), page.getRecords());
    }
}
